using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Reflection;
using System.Threading.Tasks;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;

namespace LynxWebLog.Areas.Blogs.Controllers
{
    public class BlogController : Controller
    {
        private string _blogOwner;
        private string _blogName;
        private BlogSupport blogSupport;
        protected override void Initialize(RequestContext requestContext)
        {
            ViewBag.Version = " v: " + Assembly.GetExecutingAssembly().GetName().Version;
            base.Initialize(requestContext);
            this._blogOwner = this.ControllerContext.RouteData.Values["username"].ToString();
            this._blogName = this.ControllerContext.RouteData.Values["blogname"].ToString();
            this.blogSupport = new BlogSupport();
        }
        public async Task<ActionResult> ViewCurrent()
        {
            HttpClient client = ClientUtility.BlogAPIClient();
            ViewBag.PageOwner = this._blogOwner;
            ViewBag.BlogName = this._blogName;

            HttpResponseMessage rImg = await client.GetAsync("api/BlogLayoutData/?$filter= username eq '" + this._blogOwner + "' and blogname eq '" + this._blogName + "'");
            rImg.EnsureSuccessStatusCode();
            BlogLayoutData bld = (await rImg.Content.ReadAsAsync<IEnumerable<BlogLayoutData>>()).FirstOrDefault();

            if (!string.IsNullOrEmpty(bld.header_image)) {
                ViewBag.HeaderImage = "~/../../../BlogResources/" + this._blogOwner + "/" + this._blogName + "/" + bld.header_image;
            } else {
                ViewBag.HeaderImage = "";
            }
            ViewBag.CategoryDisplay = bld.category_display;
            ViewBag.GoogleAdDisplay = bld.googlead_display;

            Post bEntry = await blogSupport.GetCurrentPost(_blogOwner, _blogName);

            HttpResponseMessage resp = await client.GetAsync("api/CategoryData");
            resp.EnsureSuccessStatusCode();

            ViewBag.CategoryDatas = (await resp.Content.ReadAsAsync<IEnumerable<CategoryData>>()).ToList();
            ViewBag.LicenseHtml = CC_License.GetLicense(bEntry.license);

            return View("BlogPost", bEntry);
        }

        public async Task<ActionResult> Post(string id)
        {
            HttpClient client = ClientUtility.BlogAPIClient();
            ViewBag.PageOwner = this._blogOwner;
            ViewBag.BlogName = this._blogName;

            HttpResponseMessage rImg = await client.GetAsync("api/BlogLayoutData/?$filter= username eq '" + this._blogOwner + "' and blogname eq '" + this._blogName + "'");
            rImg.EnsureSuccessStatusCode();
            BlogLayoutData bld = (await rImg.Content.ReadAsAsync<IEnumerable<BlogLayoutData>>()).FirstOrDefault();

            ViewBag.HeaderImage = "~/../../../BlogResources/" + this._blogOwner + "/" + this._blogName + "/" + bld.header_image;
            ViewBag.CategoryDisplay = bld.category_display;
            ViewBag.GoogleAdDisplay = bld.googlead_display;

            HttpResponseMessage resp = await client.GetAsync("api/PostData/?$filter=title eq '" + id + "' and blogid eq '" + this._blogOwner + "_" + _blogName + "'");
            resp.EnsureSuccessStatusCode();

            Post bEntry = new Post((await resp.Content.ReadAsAsync<IEnumerable<PostData>>()).FirstOrDefault());

            HttpResponseMessage rCD = await client.GetAsync("api/CategoryData");
            rCD.EnsureSuccessStatusCode();

            ViewBag.CategoryDatas = (await rCD.Content.ReadAsAsync<IEnumerable<CategoryData>>()).ToList();
            ViewBag.LicenseHtml = CC_License.GetLicense(bEntry.license);

            return View("BlogPost", bEntry);
        }

        public FilePathResult ManifestFile()
        {
            string manifestPath = HttpContext.Server.MapPath("~/App_Data/wlwmanifest.xml");

            FilePathResult result = new FilePathResult(manifestPath, "application/wlwmanifest+xml");

            return result;
        }

        public async Task<ActionResult> Index(string sort = "Name")
        {
            List<PostData> pdIndex = await blogSupport.GetPostIndex(_blogOwner, _blogName);
            ViewBag.PageOwner = this._blogOwner;
            ViewBag.BlogName = this._blogName;
            if (sort.Equals("Name")) {
                pdIndex = pdIndex.OrderBy(pd => pd.title).ToList();
            } else {
                pdIndex = pdIndex.OrderByDescending(pd => pd.dateCreated).ToList();
            }

            return View("Index", pdIndex);
        }

	}
}